package com.raptor.trustworthy.task.dao;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.raptor.trustworthy.task.mapper.ScheduleJobDaoMapper;
import com.raptor.trustworthy.task.model.ScheduleJob;
import com.raptor.trustworthycore.util.DateUtil;
import com.raptor.trustworthycore.util.RandomUtil;
import com.raptor.trustworthypojo.model.Page;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

/**
 * 定时任务dao
 *
 * @Author renhuan
 * @Date 2021-03-27 15:45
 **/
@Repository
public class ScheduleJobDao {

  @Autowired
  private ScheduleJobDaoMapper scheduleJobDaoMapper;

  //查询所有定时任务信息
  public List<ScheduleJob> getScheduleJobList() {
    List<ScheduleJob> scheduleJobList = scheduleJobDaoMapper.getScheduleJobList();

    return scheduleJobList;
  }

  //分页查询定时任务信息
  public Map<String, Object> getScheduleJobListByPage(Map<String, Object> param) {
    Page page = new Page();
    Map<String, Object> res = new HashMap<>();
    List<ScheduleJob> list = new ArrayList<>();
    if (Objects.isNull(param.get("pageSize"))){
      param.put("pageSize", "10");
    }
    if (Objects.isNull(param.get("curPage"))){
      param.put("curPage", "1");
    }
    int ps = Integer.valueOf(String.valueOf(param.get("pageSize"))).intValue();
    int cp = Integer.valueOf(String.valueOf(param.get("curPage"))).intValue();
    //设置页码
    page.setPageSize(ps);
    page.setCurPage(cp);
    PageInfo<ScheduleJob> pageInfo = PageHelper.startPage(page.getCurPage(), page.getPageSize()).
        doSelectPageInfo(() -> scheduleJobDaoMapper.getScheduleJobList());
    page.setTotalPage(pageInfo.getPages());
    page.setTotalSize(pageInfo.getTotal());
    list = pageInfo.getList();
    res.put("page", page);
    res.put("ScheduleJobList", list);
    return res;
  }

  public void insertScheduleJob(ScheduleJob scheduleJob) {
    String id = DateUtil.getNow(DateUtil.DATE_FORMATE_STRING_DEFAULT) + RandomUtil.getRandomNumString(4);
    scheduleJob.setId(Long.valueOf(id));
    scheduleJobDaoMapper.insertScheduleJob(scheduleJob);
  }

  public void updateScheduleJob(ScheduleJob scheduleJob) {
    scheduleJobDaoMapper.updateScheduleJob(scheduleJob);
  }

  public void deleteScheduleJob(ScheduleJob scheduleJob) {
    scheduleJobDaoMapper.deleteScheduleJob(scheduleJob);
  }

  public ScheduleJob getScheduleJobById(ScheduleJob scheduleJob) {
    return scheduleJobDaoMapper.getScheduleJobById(scheduleJob);
  }

  // 查询所有job

  // 启动job

}
